#include "./IMIButton.h"
//
IMIButton* IMIButton::create(const char *upFileName,const char *downFileName)
{
	IMIButton *pobSprite = new IMIButton();

	pobSprite->setTouchEnabled(true);
	CCSprite * tempUpSprite=CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(upFileName));
	pobSprite->m_upStatus=tempUpSprite->displayFrame();
	pobSprite->m_upStatus->retain();
	CCSprite * tempDownSprite=CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(downFileName));
	pobSprite->m_downStatus=tempDownSprite->displayFrame();
	pobSprite->m_downStatus->retain();

	if (pobSprite->m_upStatus && pobSprite && pobSprite->initWithSpriteFrame(pobSprite->m_upStatus))
	{
		pobSprite->autorelease();
		return pobSprite;
	}
    CC_SAFE_DELETE(pobSprite);
    return NULL;
}

bool IMIButton::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
	CCPoint touchLocation = pTouch->getLocation();
	CCPoint pos = this->getPosition();
	CCSize size = this->getContentSize();
	CCRect rect(pos.x, pos.y, size.width, size.height);
	//CCLog("---------------------------------------------------------------------------BUTTON=%s",this->m_Name.getCString());
	//CCLog("%s------%f------%f","localtouchPoint",touchLocation.x,touchLocation.y);
	if(rect.containsPoint(touchLocation))
	{
		//CCLog("**********************************************************************************BUTTON=%s",this->m_Name.getCString());
		this->setSelect(true);
		if (m_pTouchBeganListener && m_pfnTouchBeganSelector)
		{
			(m_pTouchBeganListener->*m_pfnTouchBeganSelector)(this, pTouch, pEvent);
			//CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("fx_window_open.mp3");
		}
		return true;
	}
	return false;
}

void IMIButton::setSelect(bool isSelect)
{
	//CCLog("setSelect0---------------BUTTON=%s",this->m_Name.getCString());
	//CCLog("setSelect1---------------BUTTON=%s",this->m_Name.getCString());
	this->m_IsSelect=isSelect;
	if(this->m_IsSelect)
	{
		//CCLog("setSelect2---------------BUTTON=%s",this->m_Name.getCString());
		this->setDisplayFrame(this->m_downStatus);
	}else
	{
		//CCLog("setSelect3---------------BUTTON=%s",this->m_Name.getCString());
		this->setDisplayFrame(this->m_upStatus);
	}
}

 